import ctypes

lib = ctypes.cdll.LoadLibrary(r"B:\codes\C_Cpp\DMuse\ReaperRemoteMMAP\cmake-build-debug\libReaperRemoteMMAP.dll")

command_tagname, data_tagname = b'rpr_remote_0', b'rpr_remote_1'

lib.Listen(ctypes.c_char_p(command_tagname), ctypes.c_char_p(data_tagname), 10240, 10240)

last_command = None


def send_data(b):
    lib.Clear(1)
    # RPR_ShowConsoleMsg('send: ' + str(b) + '\n')
    lib.PushMsg(b, 1, len(b))


def recv_command():
    s = b''
    p = ctypes.c_char_p(s)
    lib.GetMsg(p, 0)
    # RPR_ShowConsoleMsg('recv: ' + str(p.value) + '\n')
    return p.value


def loop():
    global last_command
    command = str(recv_command(), encoding='utf-8')

    while command and command != last_command:
        try:
            RPR_ShowConsoleMsg(command + '\n')
            send_data(str(eval(command)).encode('utf-8'))
        except Exception as e:
            RPR_ShowConsoleMsg(str(e) + '\n')
            break
        lib.Clear(0)
        last_command = command

    RPR_defer('loop()')

loop()
